#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "RPFITS.h"

#define BUFSIZE 1000

/* RPFITS commands numerical definitions to make the code more readable */
#define JSTAT_OPENFILE -3
#define JSTAT_OPENFILE_READHEADER -2
#define JSTAT_READNEXTHEADER -1
#define JSTAT_READDATA 0
#define JSTAT_CLOSEFILE 1
#define JSTAT_SKIPTOEND 2

/* RPFITS return codes definitions to make the code more readable */
#define JSTAT_UNSUCCESSFUL -1
#define JSTAT_SUCCESSFUL 0
#define JSTAT_HEADERNOTDATA 1
#define JSTAT_ENDOFSCAN 2
#define JSTAT_ENDOFFILE 3
#define JSTAT_FGTABLE 4
#define JSTAT_ILLEGALDATA 5

int main(int argc,char *argv[]){
  int i,j,k,nfiles=0,this_jstat,baseline,flag,bin,if_no,sourceno,that_jstat;
  int keep_reading,header_number,rpfits_result;
  char **filenames=NULL,source_name[BUFSIZE],datobs[BUFSIZE];
  float *vis=NULL,*wgt=NULL,ut,u,v,w;

  /* get the list of files */
  for (i=1;i<argc;i++){
    nfiles++;
    filenames=realloc(filenames,nfiles*sizeof(char*));
    if (filenames!=NULL){
      filenames[nfiles-1]=malloc(BUFSIZE*sizeof(char));
    }
    strcpy(filenames[nfiles-1],argv[i]);
  }

  /* go through them one by one and output what they have in them */
  for (i=0;i<nfiles;i++){
    strcpy(names_.file,filenames[i]);
    this_jstat=JSTAT_OPENFILE;
    rpfits_result=rpfitsin_(&this_jstat,vis,wgt,&baseline,&ut,&u,&v,
			    &w,&flag,&bin,&if_no,&sourceno);
    if (this_jstat==JSTAT_UNSUCCESSFUL){
      fprintf(stderr,"Cannot open RPFITS file %s\n",names_.file);
      continue;
    }

    printf(">>> %s:\n",names_.file);
    keep_reading=1;
    header_number=1;
    while (keep_reading){
      this_jstat=JSTAT_READNEXTHEADER;
      rpfits_result=rpfitsin_(&this_jstat,vis,wgt,&baseline,&ut,&u,&v,&w,&flag,&bin,&if_no,&sourceno);
      if (this_jstat==JSTAT_SUCCESSFUL){
	strncpy(datobs,names_.datobs,12);
	datobs[10]='\0';
	vis=realloc(vis,(if_.if_nstok[0]*if_.if_nfreq[0])*sizeof(float));
	that_jstat=JSTAT_READDATA;
	rpfits_result=rpfitsin_(&that_jstat,vis,wgt,&baseline,&ut,&u,&v,&w,&flag,&bin,&if_no,&sourceno);
 	printf("%d: %12s %f",header_number,datobs,ut);
/* 	printf(" [%s]",names_.datobs); */
	for (j=0;j<su_.n_su;j++){
	  strncpy(source_name,names_.su_name+j*16,16);
	  source_name[15]='\0';
	  if (j==0){
	    printf(" %16s",source_name);
	    for (k=0;k<if_.n_if;k++){
 	      printf(" %.3f (%.2f %dc)",doubles_.if_freq[k]/1E6,doubles_.if_bw[k]/1E6,
 		     if_.if_nfreq[k]);
	    }
	  } else {
	    printf("%12s %16s"," ",source_name);
	  }
	}
/* 	printf("\n got %d stokes and %d frequency channels in if # %d\n", */
/* 	       if_.if_nstok[0],if_.if_nfreq[0],if_no); */
/* 	vis=realloc(vis,(if_.if_nstok[0]*if_.if_nfreq[0])*sizeof(float)); */
/* 	that_jstat=JSTAT_READDATA; */
/* 	rpfits_result=rpfitsin_(&that_jstat,vis,wgt,&baseline,&ut,&u,&v,&w,&flag,&bin,&if_no,&sourceno); */
/* 	printf(" <%f>",ut); */
/* 	for (l=0;l<(if_.if_nstok[if_no]*if_.if_nfreq[if_no]);l++){ */
/* 	  printf("%f ",vis[l]); */
/* 	} */
	printf("\n");
      } else if (this_jstat==JSTAT_ENDOFFILE){
	keep_reading=0;
      }
      header_number++;
    }
    this_jstat=JSTAT_CLOSEFILE;
    rpfits_result=rpfitsin_(&this_jstat,vis,wgt,&baseline,&ut,&u,&v,&w,&flag,&bin,&if_no,&sourceno);
  }

  exit(0);
}
